<?php

/**
 * ecshop ERP v1.0
 * ----------------------------------------------------------------------------
 * http://www.phpally.com
 * Jacklee的博客 致力于php技术
 * ----------------------------------------------------------------------------
 * 作者: Jacklee
 * 邮箱: jack349392900@gmail.com
 * 创建时间: 2013-11-18
 * 最后修改时间: 2013-11-18
 */
 
define('IN_ECS', true);

require(dirname(__FILE__) . '/includes/init.php');
require_once(dirname(__FILE__) . '/includes//lib/lib_erp_common.php');
require_once(dirname(__FILE__) . '/includes/lib/lib_erp_warehouse.php');
require_once(dirname(__FILE__) . '/includes/lib/lib_erp_goods_attr.php');
require_once(dirname(__FILE__) . '/includes/lib/lib_erp_goods.php');

if ($_REQUEST['act'] == 'change_warehouse')
{
	include('../includes/cls_json.php');
	$json  = new JSON;
	
	if(!admin_priv('erp_warehouse_manage', '', false))
	{
		$result['error']=1;
		$result['message']=$_LANG['erp_no_permit'];
		die($json->encode($result));
	}
	
	$delivery_id=intval($_REQUEST['delivery_id']);
	$warehouse_id=intval($_REQUEST['warehouse_id']);
	
	//判断出库单是否存在，是否已被删除
	$delivery_info=is_delivery_exists($delivery_id);
	if(!$delivery_info)
	{
		$result['error']=3;
		$result['message']=$_LANG['erp_delivery_not_exist'];
		die($json->encode($result));	
		
	}
	
 	//判断仓库是否存在，是否已被删除
	$warehouse_info=is_warehouse_exists($warehouse_id);
	if(!$warehouse_info)
	{
		$result['error']=4;
		$result['message']=$_LANG['erp_warehouse_not_exist'];
		die($json->encode($result));
	}
	
	$admin_id=$warehouse_info['admin_id'];
	
	//修改仓库为新选的仓库
	if(lock_table($delivery_id,'delivery','edit'))
	{
		//修改仓库
		$sql="update ".$ecs->table('erp_delivery')." set warehouse_id='".$warehouse_id."' where delivery_id='".$delivery_id."'";
		$db->query($sql);		
		
		$result['error']=0;
		die($json->encode($result));		
	}
	else{
		$result['error']=-1;
		$result['message']=$_LANG['erp_delivery_no_accessibility'];
		die($json->encode($result));
	}
}


if ($_REQUEST['act'] == 'change_delivery_to')
{
	include('../includes/cls_json.php');
	$json  = new JSON;
	
	//检查是否有编辑仓库的权限
	if(!admin_priv('erp_warehouse_manage', '', false))
	{
		$result['error']=1;
		$result['message']=$_LANG['erp_no_permit'];
		die($json->encode($result));
	}
	//检查参数
	if(empty($_REQUEST['delivery_id']))
	{
		$result['error']=2;
		$result['message']=$_LANG['erp_wrong_parameter'];
		die($json->encode($result));
	}
	
	$delivery_id=intval($_REQUEST['delivery_id']);
	$delivery_to=trim($_REQUEST['delivery_to']);
	
	//判断入仓单是否存在，是否已被删除
	$delivery_info=is_delivery_exists($delivery_id);
	if(!$delivery_info)
	{
		$result['error']=3;
		$result['message']=$_LANG['erp_delivery_not_exist'];
		die($json->encode($result));	
		
	}

	//修改仓库为新选的仓库
	if(lock_table($delivery_id,'delivery','edit'))
	{
		$sql="update ".$ecs->table('erp_delivery')." set delivery_to='".$delivery_to."' where delivery_id='".$delivery_id."'";
		$db->query($sql);		
		
		$result['error']=0;
		die($json->encode($result));		
	}
	else{
		$result['error']=-1;
		$result['message']=$_LANG['erp_delivery_no_accessibility'];
		die($json->encode($result));
	}
}

elseif ($_REQUEST['act'] == 'change_delivery_style')
{
	include('../includes/cls_json.php');
	$json  = new JSON;

	if(!admin_priv('erp_warehouse_manage', '', false))
	{
		$result['error']=1;
		$result['message']=$_LANG['erp_no_permit'];
		die($json->encode($result));
	}
	
	$delivery_id=intval($_REQUEST['delivery_id']);
	$delivery_style_id=intval($_REQUEST['delivery_style_id']);
	
	//判断入仓单是否存在，是否已被删除
	$delivery_info=is_delivery_exists($delivery_id);
	if(!$delivery_info)
	{
		$result['error']=3;
		$result['message']=$_LANG['erp_delivery_not_exist'];
		die($json->encode($result));	
		
	}
	
 	//判断入库类型是否存在，是否已被删除
	$delivery_style_info=is_delivery_style_exists($delivery_style_id);
	if(!$delivery_style_info)
	{
		$result['error']=4;
		$result['message']=$_LANG['erp_delivery_style_not_exist'];
		die($json->encode($result));
	}

	if(lock_table($delivery_id,'delivery','edit'))
	{
	
		//修改入仓类型
		$sql="update ".$ecs->table('erp_delivery')." set delivery_style_id ='".$delivery_style_id."' where delivery_id='".$delivery_id."'";
		$db->query($sql);
		
		//如果是不是关联订单入库
		if($delivery_style_id!=1)
		{			
			//清空订单号
			$sql="update ".$ecs->table('erp_delivery')." set order_id='0' where delivery_id='".$delivery_id."'";
			$db->query($sql);
			//删除所有入库订单项
			$sql="delete from ".$ecs->table('erp_delivery_item')." where delivery_id='".$delivery_id."'";
			$db->query($sql);
		}
		

		$result['error']=0;
		die($json->encode($result));		
	}
	else{
		$result['error']=-1;
		$result['message']=$_LANG['erp_delivery_no_accessibility'];
		die($json->encode($result));
	}
}


elseif ($_REQUEST['act'] == 'change_order')
{
	include('../includes/cls_json.php');
	$json  = new JSON;
	
	//检查是否有编辑仓库的权限
	if(!admin_priv('erp_warehouse_manage', '', false))
	{
		$result['error']=1;
		$result['message']=$_LANG['erp_no_permit'];
		die($json->encode($result));
	}
	
	$delivery_id=intval($_REQUEST['delivery_id']);
	$order_id=intval($_REQUEST['order_id']);
	
	//判断入仓单是否存在，是否已被删除
	$delivery_info=is_delivery_exists($delivery_id);
	if(!$delivery_info)
	{
		$result['error']=3;
		$result['message']=$_LANG['erp_delivery_not_exist'];
		die($json->encode($result));	
		
	}
	
 	//判断订单是否存在，是否已被删除
	$order_info=is_sales_order_exists($order_id);
	if(!$order_info)
	{
		$result['error']=4;
		$result['message']=$_LANG['erp_order_not_exist'];
		die($json->encode($result));
	}

	if(lock_table($delivery_id,'delivery','edit'))
	{
		//修改关联订单
		$sql="update ".$ecs->table('erp_delivery')." set order_id ='".$order_id."' where delivery_id='".$delivery_id."'";
		$db->query($sql);
		
		//删除原来的入库单项
		$sql="delete from ".$ecs->table('erp_delivery_item')." where delivery_id='".$delivery_id."'";
		$db->query($sql);
		
		//新插入入库单项
		if(create_delivery_item_by_order($order_id,$delivery_id))
		{
			$result['error']=0;
			die($json->encode($result));				
		}	
		else{
			$result['error']=4;
			$result['message']=$_LANG['erp_delivery_no_stock'];
			die($json->encode($result));			
		}	
	}
	else{
		$result['error']=-1;
		$result['message']=$_LANG['erp_delivery_no_accessibility'];
		die($json->encode($result));
	}
}

elseif ($_REQUEST['act'] == 'change_delivered_qty')
{
	include('../includes/cls_json.php');
	$json  = new JSON;
	
	//检查是否有编辑仓库的权限
	if(!admin_priv('erp_warehouse_manage', '', false))
	{
		$result['error']=1;
		$result['message']=$_LANG['erp_no_permit'];
		die($json->encode($result));
	}
	
	$delivery_id=intval($_REQUEST['delivery_id']);
	$delivery_item_id=intval($_REQUEST['delivery_item_id']);
	$delivered_qty=intval($_REQUEST['delivered_qty']);
	
	//判断入仓单是否存在，是否已被删除
	$delivery_info=is_delivery_exists($delivery_id);
	if(!$delivery_info)
	{
		$result['error']=3;
		$result['message']=$_LANG['erp_delivery_not_exist'];
		die($json->encode($result));	
		
	}
	
 	//判断入库单项是否存在，是否已被删除
	$delivery_item_info=is_delivery_item_exists($delivery_item_id);
	if(!$delivery_item_info)
	{
		$result['error']=4;
		$result['message']=$_LANG['erp_delivery_item_not_exist'];
		die($json->encode($result));
	}
	else{
		$goods_id=$delivery_item_info['goods_id'];
		$attr_id=$delivery_item_info['attr_id'];
	}
	
	if(lock_table($delivery_id,'delivery','edit'))
	{
		$sql="select warehouse_id from ".$ecs->table('erp_delivery')." where  delivery_id='".$delivery_id."'";
		$warehouse_id=$GLOBALS['db']->getOne($sql);
		
		$goods_stock=get_goods_stock($warehouse_id,$goods_id,$attr_id);
		
		if($delivered_qty>$goods_stock)
		{
			$result['error']=1;
			$result['message']=sprintf($_LANG['erp_delivery_stock_not_enough'],$goods_stock);
			die($json->encode($result));				
		}
		else{

			//更改实发数
			$sql="update ".$ecs->table('erp_delivery_item')." set delivered_qty='".$delivered_qty."',account_due=price*".$delivered_qty." where delivery_item_id='".$delivery_item_id."'";
			$db->query($sql);
			$result['error']=0;
			die($json->encode($result));			
			
		}
	}
	else{
		$result['error']=-1;
		$result['message']=$_LANG['erp_delivery_no_accessibility'];
		die($json->encode($result));
	}
	
}

elseif ($_REQUEST['act'] == 'delete_delivery_item')
{
	include('../includes/cls_json.php');
	$json  = new JSON;
	
	//检查是否有编辑仓库的权限
	if(!admin_priv('erp_warehouse_manage', '', false))
	{
		$result['error']=1;
		$result['message']=$_LANG['erp_no_permit'];
		die($json->encode($result));
	}

	
	$delivery_id=intval($_REQUEST['delivery_id']);
	$delivery_item_id=intval($_REQUEST['delivery_item_id']);
	
	//判断入仓单是否存在，是否已被删除
	$delivery_info=is_delivery_exists($delivery_id);
	if(!$delivery_info)
	{
		$result['error']=3;
		$result['message']=$_LANG['erp_delivery_not_exist'];
		die($json->encode($result));	
		
	}
	
 	//判断入库单项是否存在，是否已被删除
	$delivery_item_info=is_delivery_item_exists($delivery_item_id);
	if(!$delivery_item_info)
	{
		$result['error']=4;
		$result['message']=$_LANG['erp_delivery_item_not_exist'];
		die($json->encode($result));
	}

	if(lock_table($delivery_id,'delivery','edit'))
	{
		$sql="delete from ".$ecs->table('erp_delivery_item')." where delivery_item_id='".$delivery_item_id."'";
		$db->query($sql);
		$result['error']=0;
		die($json->encode($result));
	}
	else{
		$result['error']=-1;
		$result['message']=$_LANG['erp_delivery_no_accessibility'];
		die($json->encode($result));
	}	
}

elseif ($_REQUEST['act'] == 'delete_delivery')
{
	include('../includes/cls_json.php');
	$json  = new JSON;
	
	//检查是否有编辑仓库的权限
	if(!admin_priv('erp_warehouse_manage', '', false))
	{
		$result['error']=1;
		$result['message']=$_LANG['erp_no_permit'];
		die($json->encode($result));
	}
	//检查参数
	if(empty($_REQUEST['delivery_id']))
	{
		$result['error']=2;
		$result['message']=$_LANG['erp_wrong_parameter'];
		die($json->encode($result));
	}
	
	$delivery_id=intval($_REQUEST['delivery_id']);
	
	//判断入仓单是否存在，是否已被删除
	$delivery_info=is_delivery_exists($delivery_id);
	if(!$delivery_info)
	{
		$result['error']=3;
		$result['message']=$_LANG['erp_delivery_not_exist'];
		die($json->encode($result));
	}
	
	if(lock_table($delivery_id,'delivery','delete'))
	{
		//删除所有该入库单下的入库单项
		$sql="delete from ".$ecs->table('erp_delivery_item')." where delivery_id='".$delivery_id."'";
		$db->query($sql);
		
		$sql="delete from ".$ecs->table('erp_delivery')." where delivery_id='".$delivery_id."'";
		$db->query($sql);
		$result['error']=0;
		die($json->encode($result));
	}
	else{
		$result['error']=-1;
		$result['message']=$_LANG['erp_delivery_no_accessibility'];
		die($json->encode($result));
	}	
}

elseif ($_REQUEST['act'] == 'check_goods_sn')
{
	include('../includes/cls_json.php');
	$json  = new JSON;	
	
	$goods_sn=trim($_REQUEST['goods_sn']);
		
	if(!check_goods_sn($goods_sn))
	{
		$result['error']=1;
		$result['message']=$_LANG['erp_delivery_wrong_goods_sn'];
		die($json->encode($result));		
	}
	else{
		$sql="select goods_id from ".$GLOBALS['ecs']->table('goods')." where goods_sn='".$goods_sn."'";
		$goods_id=$GLOBALS['db']->getOne($sql);
		
		$goods_attr=get_goods_attr($goods_id);

		$smarty->assign('goods_attr', $goods_attr);	
		$attr_info=$smarty->fetch('erp_select_goods_attr.htm');

		$result['attr_info']=$attr_info;
		$result['attr_num']=count($goods_attr);		
		$result['error']=0;
		
		die($json->encode($result));			
	}
}


elseif ($_REQUEST['act'] == 'update_delivery')
{
	include('../includes/cls_json.php');
	$json  = new JSON;	
	
	//检查是否有编辑仓库的权限
	if(!admin_priv('erp_warehouse_manage', '', false))
	{
		$result['error']=1;
		$result['message']=$_LANG['erp_no_permit'];
		die($json->encode($result));
	}

	$delivery_id=isset($_REQUEST['delivery_id'])? intval($_REQUEST['delivery_id']):0;	
	$goods_sn=isset($_REQUEST['goods_sn'])? trim($_REQUEST['goods_sn']):'';
	$ids=isset($_REQUEST['ids'])? trim($_REQUEST['ids']):'';
	
	if(empty($_REQUEST['goods_qty']))
	{
		$goods_qty=1;
	}
	else{
		$goods_qty=intval($_REQUEST['goods_qty']);
	}
		
	//检查参数
	if(empty($goods_sn))
	{
		$result['error']=1;
		$result['message']=$_LANG['erp_delivery_goods_sn_required'];
		die($json->encode($result));				
	}
	
	//判断产品是否存在
	if(!check_goods_sn($goods_sn))
	{
		$result['error']=1;
		$result['message']=$_LANG['erp_delivery_wrong_goods_sn'];
		die($json->encode($result));		
	}				
		
	//判断入仓单是否存在，是否已被删除
	$delivery_info=is_delivery_exists($delivery_id);
	if(!$delivery_info)
	{
		$result['error']=3;
		$result['message']=$_LANG['erp_delivery_not_exist'];
		die($json->encode($result));
	}
	else{
		$warehouse_id=$delivery_info['warehouse_id'];
	}
	
	//获取产品Id
	$sql="select goods_id from ".$ecs->table('goods')." where goods_sn='".$goods_sn."'";
	$goods_id=$db->getOne($sql);
	
	if(empty($ids))
	{
			//获取产品库存数
			$stock_qty=get_goods_stock($warehouse_id,$goods_id,'');

			//判断入仓单是否已存在，如已存在，更新数量
			$sql="select delivery_item_id,delivered_qty from ".$ecs->table('erp_delivery_item')." where delivery_id='".$delivery_id."' and goods_id='".$goods_id."'";
			$delivery_item_info=$db->getRow($sql);

			if(empty($delivery_item_info))
			{
					if($goods_qty>$stock_qty)
					{
						$result['error']=1;
						$result['message']=sprintf($_LANG['erp_delivery_stock_not_enough'],$stock_qty);
						die($json->encode($result));							
					}
					else{
						$sql="insert into ".$ecs->table('erp_delivery_item')." set delivery_id='".$delivery_id."',goods_id='".$goods_id."', expected_qty='0',delivered_qty='".$goods_qty."'";			
						$db->query($sql);
						$result['error']=0;
						die($json->encode($result));							
					}
			}
			else{
				$delivery_item_id=$delivery_item_info['delivery_item_id'];
				$delivered_qty=$delivery_item_info['delivered_qty'];
				
				if($delivered_qty+$goods_qty>$stock_qty)
				{
						$result['error']=1;
						$result['message']=sprintf($_LANG['erp_delivery_stock_not_enough'],$stock_qty);
						die($json->encode($result));							
					}	
				else{
					$sql="update ".$ecs->table('erp_delivery_item')." set delivered_qty=delivered_qty+'".$goods_qty."' where delivery_item_id='".$delivery_item_id."'";
					$db->query($sql);
					$result['error']=0;
					die($json->encode($result));						
				}	
			}			
	}
	else{
		//去除最后一个逗号
		$ids=substr($ids,0,strlen($ids)-1);	
		//获取产品属性Id
		$attr_id=get_attr_id($goods_id,$ids);	
	
		//获取产品库存数
		$stock_qty=get_goods_stock($warehouse_id,$goods_id,$attr_id);	
		
		//判断入仓单是否已存在，如已存在，更新数量
		$sql="select delivery_item_id,delivered_qty from ".$ecs->table('erp_delivery_item')." where delivery_id='".$delivery_id."' and goods_id='".$goods_id."' and attr_id='".$attr_id."'";
		$delivery_item_info=$db->getRow($sql);
			
		if(empty($delivery_item_info))
		{
					if($goods_qty>$stock_qty)
					{
						$result['error']=1;
						$result['message']=sprintf($_LANG['erp_delivery_stock_not_enough'],$stock_qty);
						die($json->encode($result));							
					}					
					else{
						$sql="insert into ".$ecs->table('erp_delivery_item')." set delivery_id='".$delivery_id."',goods_id='".$goods_id."',attr_id='".$attr_id."', expected_qty='0',delivered_qty='".$goods_qty."'";			
						$db->query($sql);
						$result['error']=0;
						die($json->encode($result));						
					}
		}
		else{
				$delivery_item_id=$delivery_item_info['delivery_item_id'];
				$delivered_qty=$delivery_item_info['delivered_qty'];
				
				if($delivered_qty+$goods_qty>$stock_qty)
				{
						$result['error']=1;
						$result['message']=sprintf($_LANG['erp_delivery_stock_not_enough'],$stock_qty);
						die($json->encode($result));							
					}				
				else{
					$sql="update ".$ecs->table('erp_delivery_item')." set delivered_qty=delivered_qty+'".$goods_qty."' where delivery_item_id='".$delivery_item_id."'";
					$db->query($sql);
					$result['error']=0;
					die($json->encode($result));						
				}
		}						
	}
}

elseif ($_REQUEST['act'] == 'post_delivery')
{
	include('../includes/cls_json.php');
	$json  = new JSON;	
	
	//检查是否有编辑仓库的权限
	if(!admin_priv('erp_warehouse_manage', '', false))
	{
		$result['error']=1;
		$result['message']=$_LANG['erp_no_permit'];
		die($json->encode($result));
	}

	$delivery_id=intval($_REQUEST['delivery_id']);

	//判断入仓单是否存在，是否已被删除
	$delivery_info=is_delivery_exists($delivery_id);
	if(!$delivery_info)
	{
		$result['error']=3;
		$result['message']=$_LANG['erp_delivery_not_exist'];
		die($json->encode($result));
	}
	//检查入库单是否完善
	$error=check_delivery_integrity($delivery_id);
	if($error==1)
	{
		$result['error']=4;
		$result['message']=$_LANG['erp_delivery_without_choosing_order'];
		die($json->encode($result));
		}
	elseif($error==2)
	{
		$result['error']=5;
		$result['message']=$_LANG['erp_delivery_without_delivery_to'];
		die($json->encode($result));			
	}
	elseif($error==3)
	{
		$result['error']=6;
		$result['message']=$_LANG['erp_delivery_without_delivery_item'];
		die($json->encode($result));			
	}
	elseif($error==4)
	{
		$result['error']=7;
		$result['message']=$_LANG['erp_delivery_without_delivery_qty'];
		die($json->encode($result));			
	}		
	if(lock_table($delivery_id,'delivery','post_to_approve'))
	{
		$sql="update ".$ecs->table('erp_delivery')." set status='2',post_by='".erp_get_admin_id()."',post_time='".time()."' where delivery_id='".$delivery_id."'";
		if($db->query($sql))
		{
			$result['error']=0;
			die($json->encode($result));			
		}
	}
	else{
		$result['error']=-1;
		$result['message']=$_LANG['erp_delivery_no_accessibility'];
		die($json->encode($result));
	}
}


elseif ($_REQUEST['act'] == 'add_delivery')
{
	include('../includes/cls_json.php');
	$json  = new JSON;	
	
	if(!admin_priv('erp_warehouse_manage', '', false))
	{
		$result['error']=1;
		$result['message']=$_LANG['erp_no_permit'];
		die($json->encode($result));
	}
	
	$delivery_id=add_delivery();
	
	if($delivery_id==-1)
	{
		$result['error']=1;
		$result['message']=$GLOBALS['_LANG']['erp_delivery_without_valid_warehouse'];
		die($json->encode($result));
	}	
	else{
		$result['error']=0;
		$result['delivery_id']=$delivery_id;
		die($json->encode($result));		
	}	
}

elseif ($_REQUEST['act'] == 'withdrawal_to_edit')
{
	include('../includes/cls_json.php');
	$json  = new JSON;	
	
	if(!admin_priv('erp_warehouse_approve', '', false))
	{
		$result['error']=1;
		$result['message']=$_LANG['erp_no_permit'];
		die($json->encode($result));
	}
	
	$delivery_id=intval($_REQUEST['delivery_id']);
	
	$sql="update ".$ecs->table('erp_delivery')." set status='1' where delivery_id='".$delivery_id."'";
		
	$db->query($sql);		
	
	$result['error']=0;
	die($json->encode($result));	
}

elseif ($_REQUEST['act'] == 'approve_pass')
{
	include('../includes/cls_json.php');
	$json  = new JSON;	
	
	if(!admin_priv('erp_warehouse_approve', '', false))
	{
		$result['error']=1;
		$result['message']=$_LANG['erp_no_permit'];
		die($json->encode($result));
	}
	
	$delivery_id=intval($_REQUEST['delivery_id']);
	$approve_remark=trim($_REQUEST['approve_remark']);

	if(lock_table($delivery_id,'delivery','approve'))
	{
		$delivery_info=is_delivery_exists($delivery_id);
		
		$delivery_style_id=$delivery_info['delivery_style_id'];
		$warehouse_id=$delivery_info['warehouse_id'];
		$order_id=$delivery_info['order_id'];
		$delivery_sn=$delivery_info['delivery_sn'];
		$delivery_to=$delivery_info['delivery_to'];
	
		if(!empty($order_id))
		{
			$sql="select order_sn from ".$ecs->table('order_info')." where order_id='".$order_id."'";
			$order_sn=$db->getOne($sql);				
		}
		else{
			$order_sn='';
		}
		
		$delivery_item_info=get_delivery_item_info($delivery_id);
		
		//如果是关联订单出库，修改相应订单的已出库数
		if($delivery_style_id==1)
		{
			foreach($delivery_item_info as $delivery_item)
			{
					$order_item_id=$delivery_item['order_item_id'];
					$delivered_qty=$delivery_item['delivered_qty'];
					
					$sql="update ".$ecs->table('order_goods')." set delivery_qty=delivery_qty+".$delivered_qty." where rec_id='".$order_item_id."'";
					$db->query($sql);
			}
		}
		
		//更新出入库序时薄
		foreach($delivery_item_info as $delivery_item)
		{
			$goods_id=$delivery_item['goods_id'];
			$attr_id=$delivery_item['attr_id'];
			$qty=$delivery_item['delivered_qty'];
			$price=$delivery_item['price'];
			
			$sql="insert into ".$ecs->table('erp_stock')." set goods_id='".$goods_id."',goods_attr_id='".$attr_id."',qty='".(0-$qty)."',price='".$price."',amount=qty*price,act_time='".time()."',warehouse_id='".$warehouse_id."'";
			$sql.=", w_d_name='".$delivery_to."', w_d_style_id='".$delivery_style_id."',order_sn='".$order_sn."',stock_style='d',w_d_sn='".$delivery_sn."'";
			
			$db->query($sql);
		}
		
		//如果仓库是成品仓，那么更新网店库存
		if($warehouse_id==1)
		{
			foreach($delivery_item_info as $delivery_item)
			{
				$goods_id=$delivery_item['goods_id'];
				$attr_id=$delivery_item['attr_id'];
				$qty=$delivery_item['delivered_qty'];
			
				//均码
				if(empty($attr_id))
				{
					$sql="update ".$ecs->table('goods')." set goods_number=goods_number-".$qty." where goods_id='".$goods_id."'";
					$db->query($sql);
					
					$sql="update ".$ecs->table('erp_goods_attr')." set goods_qty =goods_qty -".$qty." where goods_id='".$goods_id."' and goods_attr_id_list='$$'";
					$db->query($sql);						
				}
				else{
					$sql="update ".$ecs->table('erp_goods_attr')." set goods_qty=goods_qty-".$qty." where goods_id='".$goods_id."' and attr_id='".$attr_id."'";
					$db->query($sql);											
				}
			}	
		}
		
		//如果仓库是成品仓,更新网店总库存
		if($warehouse_id==1)
		{
			foreach($delivery_item_info as $delivery_item)
			{
				$goods_id=$delivery_item['goods_id'];
				$sql="select sum(goods_qty) as total_qty from ".$ecs->table('erp_goods_attr')." where goods_id='".$goods_id."'";
				$total_qty=	$db->getOne($sql);	
				$sql="update ".$ecs->table('goods')." set goods_number='".$total_qty."' where goods_id='".$goods_id."'";	
				$db->query($sql);
			}			
		}
		
		//设置出库单状态为3
		$sql="update ".$ecs->table('erp_delivery')." set status='3',approve_time='".time()."',approved_by='".erp_get_admin_id()."',approve_remark='".$approve_remark."' where delivery_id='".$delivery_id."'";
		
		if($db->query($sql))
		{
			$result['error']=0;
			die($json->encode($result));			
		}
	}
	else{
		$result['error']=-1;
		$result['message']=$_LANG['erp_delivery_no_accessibility'];
		die($json->encode($result));
	}
}


elseif ($_REQUEST['act'] == 'approve_reject')
{
	include('../includes/cls_json.php');
	$json  = new JSON;	
	
	if(!admin_priv('erp_warehouse_approve', '', false))
	{
		$result['error']=1;
		$result['message']=$_LANG['erp_no_permit'];
		die($json->encode($result));
	}
	
	if(empty($_REQUEST['delivery_id']))
	{
		$result['error']=2;
		$result['message']=$_LANG['erp_wrong_parameter'];
		die($json->encode($result));
	}

	$delivery_id=intval($_REQUEST['delivery_id']);
	$approve_remark=trim($_REQUEST['approve_remark']);

	if(lock_table($delivery_id,'delivery','approve'))
	{

		$sql="update ".$ecs->table('erp_delivery')." set status='5',approve_remark='".$approve_remark."' where delivery_id='".$delivery_id."'";
		
		if($db->query($sql))
		{
			$result['error']=0;
			die($json->encode($result));			
		}
	}
	else{
		$result['error']=-1;
		$result['message']=$_LANG['erp_delivery_no_accessibility'];
		die($json->encode($result));
	}
}



elseif ($_REQUEST['act'] == 'batch_delivery')
{
	include('../includes/cls_json.php');
	$json  = new JSON;	
	
	if(!admin_priv('erp_warehouse_manage', '', false))
	{
		$result['error']=1;
		$result['message']=$_LANG['erp_no_permit'];
		die($json->encode($result));
	}

	$delivery_id=intval($_REQUEST['delivery_id']);

	if(isset($_FILES['file_batch_delivery']))
	{
		$upload_file_info=$_FILES['file_batch_delivery'];
		//判断文件类型
		if($upload_file_info['type']!='text/plain')
		{
			$href="erp_warehouse_manage.php?act=edit_delivery&id=".$delivery_id;
			$text=$_LANG['erp_delivery_return'];
			$link[] = array('href' => $href, 'text' => $text);

   			sys_msg($_LANG['erp_delivery_only_text_file'], 0, $link);
		}
		//转移并重命名文件
		$souce_file=$upload_file_info['tmp_name'];
		$upload_file=format_file($souce_file);
		
		//检查是否是合法的入库文件
		$delivery_info=check_file($upload_file);
		
		if(!is_array($delivery_info))
		{
			if($delivery_info==1)
			{
				$href="erp_warehouse_manage.php?act=edit_delivery&id=".$delivery_id;
				$text=$_LANG['erp_delivery_return'];
				$link[] = array('href' => $href, 'text' => $text);

   				sys_msg($_LANG['erp_delivery_invalid_file'], 0, $link);					
			}
			elseif($delivery_info==2)
			{
				$href="erp_warehouse_manage.php?act=edit_delivery&id=".$delivery_id;
				$text=$_LANG['erp_delivery_return'];
				$link[] = array('href' => $href, 'text' => $text);

   				sys_msg($_LANG['erp_delivery_invalid_goods_sn'], 0, $link);					
			}
			elseif($delivery_info==3)
			{
				$href="erp_warehouse_manage.php?act=edit_delivery&id=".$delivery_id;
				$text=$_LANG['erp_delivery_return'];
				$link[] = array('href' => $href, 'text' => $text);

   				sys_msg($_LANG['erp_delivery_invalid_goods_size'], 0, $link);					
			}
		}
		else{
			foreach($delivery_info as $delivery_item)
			{
				$goods_sn=$delivery_item['goods_info']['goods_sn'];
				$goods_size=$delivery_item['goods_info']['goods_size'];
				$delivery_qty=$delivery_item['qty'];
				
				$sql="select goods_id from ".$ecs->table('goods')." where goods_sn='".$goods_sn."'";
				$goods_id=$db->getOne($sql);
				
				if(lock_table($delivery_id,'delivery','edit'))
				{
					$sql="select delivery_item_id from ".$ecs->table('erp_delivery_item')." where delivery_id='".$delivery_id."' and goods_id='".$goods_id ."' and goods_size='".$goods_size."'";
					$delivery_item_id=$db->getOne($sql);
					if(!empty($delivery_item_id))
					{
						$sql="update ".$ecs->table('erp_delivery_item')." set delivered_qty=delivered_qty+".$delivery_qty." where delivery_item_id='".$delivery_item_id."'";
						$db->query($sql);
					}
					else{
						$sql="insert into ".$ecs->table('erp_delivery_item')." set delivery_id=".$delivery_id.",goods_id='".$goods_id."',goods_size='".$goods_size."',delivered_qty=".$delivery_qty;
						$db->query($sql);
					}
				}
				else{
					$href="erp_warehouse_manage.php?act=edit_delivery&id=".$delivery_id;
					$text=$_LANG['erp_delivery_return'];
					$link[] = array('href' => $href, 'text' => $text);

   					sys_msg($_LANG['erp_delivery_no_accessibility'], 0, $link);	
				}				
			}
		}			
	}
		$href="erp_warehouse_manage.php?act=edit_delivery&id=".$delivery_id;
		$text=$_LANG['erp_delivery_return'];
		$link[] = array('href' => $href, 'text' => $text);

   		sys_msg($_LANG['erp_delivery_batch_delivery_success'], 0, $link);		
}

?>